<?php

namespace Core;

!defined('APP_PATH') && exit(1);

/**
 * Description of Config
 *
 * @author lxc
 */
class Config
{

	public static $config = [];
	private static $handel = null;

	private function __construct()
	{
		$conf_path = APP_PATH . '/conf.ini';
		if (is_file($conf_path))
			self::$config = parse_ini_file($conf_path, true) ?: [];
	}

	/**
	 * index
	 * @return type
	 */
	public static function getInstance()
	{
		if (is_null(self::$handel))
			self::$handel = new self();
		return self::$handel;
	}

	/**
	 * get
	 * @param type $key
	 * @return type
	 */
	public function get($key = '')
	{
		if (empty($key))
			return self::$config;

		if (strpos($key, '.') === false)
			return self::$config[$key] ?: null;
		list($key1, $key2) = explode('.', $key);
		if (!isset(self::$config[$key1]))
			return null;
		return self::$config[$key1][$key2] ?: null;
	}

	/**
	 * set
	 * @param type $key
	 * @param type $val
	 * @return boolean
	 */
	public function set($key, $val)
	{
		if (strpos($key, '.') === false) {
			self::$config[$key] = $val;
			return true;
		}
		list($key1, $key2) = explode('.', $key);
		self::$config[$key1][$key2] = $val;
		return true;
	}

}
